<?php

namespace App\Http\Controllers\Wx;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\UserCoupon;
use App\Http\Requests\ApiRequest;

class UserCouponController extends Controller
{
    public function index(ApiRequest $request)
    {
    	$rs['status'] = true;
    	$where['user_id'] = $request->user_id; 
        $where['is_used'] = 0;
    	$list = UserCoupon::where($where)
                            ->with('coupon')                   
                            ->paginate(10);
    	$rs['data'] = $list;      
        return response()->json($rs);
    }

    

    public function store(ApiRequest $request)
    {
    	$rs['status']      = true;         
        $data = $request->pure();
       
        $where['user_id']   = $request->user_id; 
        $where['coupon_id'] = $request->coupon_id;     
        $uc = UserCoupon::where($where)->first();
        if($uc) {
            $rs['msg'] = '优惠券已被领取啦';
            return response()->json($rs);
        }
        
        $date = strtotime("+7 day", time());
        $data['end_date'] = date('Y-m-d H:i:s',$date);
        $flag = UserCoupon::create($data); 
        $rs['data'] = $flag;      
        return response()->json($rs);
    }

   

    

   
}
